package rdfdistiller;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import usefulstuffs.Encoder;

public class RDFDistiller {

	private static final String RDFDISTILLER_URL = "http://rdf.greggkellogg.net/distiller";
	
	
	public static InputStream distillRDF(String urlString, String inputFormat, String outputFormat) throws Exception {
	    
		/**
		 * We made these lines because inputFormat and outputFormat
		 * are variables which we use to make one string
		 * which includes them like standard parts.
		 */
		if (inputFormat == null || inputFormat.equals("") )
			inputFormat = "rdfagraph";
		if ( outputFormat == null || outputFormat.equals("") )
			outputFormat = "rdfxml";
		
		/** Making a StringBuilder which will be equivivalent with 
		 * form which Distiller know to use.
		 */
		StringBuilder sb = new StringBuilder();
		sb.append(RDFDISTILLER_URL);
		sb.append("?format=").append( outputFormat + "&" + inputFormat );
	    sb.append("&uri=").append( Encoder.encode(urlString) );
	    
	    URL requestURL = new URL( sb.toString() );
	    HttpURLConnection conn = (HttpURLConnection)requestURL.openConnection();
        conn.setRequestMethod("GET");
        
        return conn.getInputStream();
        
	}
}
